package com.ruoyi.recycle.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.recycle.domain.RecycleCommodity;
import com.ruoyi.recycle.service.IRecycleCommodityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 回收商品Controller
 *
 * @author ruoyi
 * @date 2022-05-22
 */
@RestController
@RequestMapping("/recycleCommodity")
@Api(tags = "回收商品")
public class RecycleCommodityController extends BaseController {
    @Autowired
    private IRecycleCommodityService recycleCommodityService;

    /**
     * 查询回收商品列表
     */
    @RequiresPermissions("recycle:recycleCommodity:list")
    @GetMapping("/list")
    @ApiOperation("查询回收商品列表")
    public TableDataInfo list(RecycleCommodity recycleCommodity) {
        startPage();
        List<RecycleCommodity> list = recycleCommodityService.selectRecycleCommodityList(recycleCommodity);
        return getDataTable(list);
    }

    /**
     * 导出回收商品列表
     */
    @RequiresPermissions("recycle:recycleCommodity:export")
    @Log(title = "回收商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出回收商品列表")
    public void export(HttpServletResponse response, RecycleCommodity recycleCommodity) {

        List<RecycleCommodity> list = recycleCommodityService.selectRecycleCommodityList(recycleCommodity);
        ExcelUtil<RecycleCommodity> util = new ExcelUtil<RecycleCommodity>(RecycleCommodity.class);
        util.exportExcel(response, list, "回收商品数据");
    }

    /**
     * 获取回收商品详细信息
     */
    @RequiresPermissions("recycle:recycleCommodity:query")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取回收商品详细信息")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(recycleCommodityService.selectRecycleCommodityById(id));
    }

    /**
     * 新增回收商品
     */
    @RequiresPermissions("recycle:recycleCommodity:add")
    @Log(title = "回收商品", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增回收商品")
    public AjaxResult add(@RequestBody RecycleCommodity recycleCommodity) {
        return toAjax(recycleCommodityService.insertRecycleCommodity(recycleCommodity));
    }

    /**
     * 修改回收商品
     */
    @RequiresPermissions("recycle:recycleCommodity:edit")
    @Log(title = "回收商品", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改回收商品")
    public AjaxResult edit(@RequestBody RecycleCommodity recycleCommodity) {
        return toAjax(recycleCommodityService.updateRecycleCommodity(recycleCommodity));
    }

    /**
     * 删除回收商品
     */
    @RequiresPermissions("recycle:recycleCommodity:remove")
    @Log(title = "回收商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除回收商品")
    @ApiImplicitParam(name = "ids", value = "id集合", dataType = "List")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(recycleCommodityService.deleteRecycleCommodityByIds(ids));
    }
}
